﻿using RimWorld;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using Verse;
using Verse.Noise;

namespace SZ.Mech
{
    [StaticConstructorOnStartup]
    public class SZ_Projectile_Explosive : Projectile
    {
        private int ticksToDetonation;
        //private int age;
        private float hide = 1;

        private const float reduce = 0.01f;
        //protected virtual float LightningBrightness
        //{
        //    get
        //    {
        //        return (float)age / 20f;
        //    }
        //}

        public override void ExposeData()
        {
            base.ExposeData();
            Scribe_Values.Look(ref ticksToDetonation, "ticksToDetonation", 0);
            Scribe_Values.Look(ref hide, "hide", 1);
            //Scribe_Values.Look<bool>(ref drawLight, "drawLight");
        }


        public override void Tick()
        {
            base.Tick();
            if (hide == 1)
            {
                LightningMat.color = new Color(0.95f, 0.35f, 0.57f, 1);
            }

            if (hide != 0)
            {
                hide -= reduce;
            }
            if (hide <= 0)
            {
                Destroy();
                LightningMat.color = new Color(1f, 1f, 1f, 1);
               
            }
            //if (ticksToDetonation > 0)
            //{
            //    ticksToDetonation--;
            //    if (ticksToDetonation <= 0)
            //    {
            //        if (age == 0)
            //        {
            //            Explode();
            //        }
            //        age++;
            //    }
            //}


        }

        protected override void Impact(Thing hitThing, bool blockedByShield = false)
        {

            if (blockedByShield || def.projectile.explosionDelay == 0)
            {
                landed = true;
                Explode();
                hide -= reduce;
                return;
            }
        }

        private static readonly Material LightningMat = MatLoader.LoadMat("Weather/LightningBolt");
        private Material mat;

        //public Material LightMat
        //{ 
        //    get
        //    {
        //        if(mat==null)
        //        {
        //            mat = new Material(LightningMat.shader);
        //        }
        //        return mat;
        //    }
        //}

        //private static readonly Material LightningMat = MaterialPool.MatFrom("Weather/LightningBolt", ShaderDatabase.Transparent);
        private Mesh boltMesh;

        protected override void DrawAt(Vector3 drawLoc, bool flip = false)
        {
            base.DrawAt(drawLoc, flip);
            if (hide != 1)
            {
                DrawLight();
            }
        }

        protected void DrawLight()
        {
            Matrix4x4 matrix = default(Matrix4x4);
            Vector3 drawPos = this.Position.ToVector3();
            matrix.SetTRS(drawPos, Quaternion.AngleAxis(0, Vector3.up), new Vector3(1, 1, 1));

            //Graphics.DrawMesh(MeshPool.plane10, matrix, LightningMat, 0);
            Color casheColor = LightningMat.color;
            Material alphaMt = FadedMaterialPool.FadedVersionOf(LightningMat, hide);
            casheColor.a = alphaMt.color.a;
            alphaMt.color = casheColor;
            //Messages.Message($"透明度{alphaMt.color.a}", MessageTypeDefOf.TaskCompletion);
            Graphics.DrawMesh(boltMesh, matrix, alphaMt, 100);
        }




        protected void Explode()
        {
            Map map = base.Map;
            //Destroy();
            if (def.projectile.explosionEffect != null)
            {
                Effecter effecter = def.projectile.explosionEffect.Spawn();
                effecter.Trigger(new TargetInfo(base.Position, map), new TargetInfo(base.Position, map));
                effecter.Cleanup();
            }
            boltMesh = LightningBoltMeshPool.RandomBoltMesh;
            //避免爆炸波及子弹，主要改了忽略子弹
            GenExplosion.DoExplosion(base.Position, map, def.projectile.explosionRadius, def.projectile.damageDef, launcher, DamageAmount, ArmorPenetration, def.projectile.soundExplode, equipmentDef, def, intendedTarget.Thing, def.projectile.postExplosionSpawnThingDef, postExplosionSpawnThingDefWater: def.projectile.postExplosionSpawnThingDefWater, postExplosionSpawnChance: def.projectile.postExplosionSpawnChance, postExplosionSpawnThingCount: def.projectile.postExplosionSpawnThingCount, postExplosionGasType: def.projectile.postExplosionGasType, preExplosionSpawnThingDef: def.projectile.preExplosionSpawnThingDef, preExplosionSpawnChance: def.projectile.preExplosionSpawnChance, preExplosionSpawnThingCount: def.projectile.preExplosionSpawnThingCount, applyDamageToExplosionCellsNeighbors: def.projectile.applyDamageToExplosionCellsNeighbors, chanceToStartFire: def.projectile.explosionChanceToStartFire, damageFalloff: def.projectile.explosionDamageFalloff, direction: origin.AngleToFlat(destination), ignoredThings: new List<Thing>() { this }, affectedAngle: null, doVisualEffects: true, propagationSpeed: def.projectile.damageDef.expolosionPropagationSpeed, excludeRadius: 0f, doSoundEffects: true, screenShakeFactor: def.projectile.screenShakeFactor);
            GenExplosion.DoExplosion(base.Position, map, def.projectile.explosionRadius, DamageDefOf.Flame, launcher);
            GenExplosion.DoExplosion(base.Position, map, def.projectile.explosionRadius, DamageDefOf.EMP, launcher);
            Vector3 drawPos = this.Position.ToVector3();
            FleckMaker.ThrowSmoke(drawPos, this.Map, 1.5f);
            FleckMaker.ThrowMicroSparks(drawPos, this.Map);//投掷火花
            FleckMaker.ThrowLightningGlow(drawPos, this.Map, 1.5f);//投掷闪电光
        }
    }
}
